package com.tz.crm.dao.impl;

import com.tz.crm.dao.IndexpictureDao;
import com.tz.crm.entity.IndexPicture;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/1/28.
 */
@Repository
@Transactional
public class IndexpictureDaoImpl extends BaseDaoImpl<IndexPicture, String> implements IndexpictureDao {

    @Override
    public void upload(File file, String title) throws Exception {
        FileInputStream fis = new FileInputStream(file);
        Blob data = getSession().getLobHelper().createBlob(fis, file.length());
        IndexPicture indexPicture = new IndexPicture();
        indexPicture.setContent(data);
        indexPicture.setTitle(title);
        save(indexPicture);
    }

    @Override
    public InputStream download(IndexPicture indexPicture) throws SQLException {
        InputStream is = indexPicture.getContent().getBinaryStream();
        return is;

    }
}
